﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCode.Project.Domain.Businesses.Sms
{
    /// <summary>
    /// 短信模板基类
    /// </summary>
    public abstract class SmsTemplateBase
    {
        /// <summary>
        /// 参数字典
        /// </summary>
        protected Dictionary<string, object> ParamDict = new Dictionary<string, object>();

        /// <summary>
        /// 短信模板
        /// </summary>
        protected string Template { get; set; }

        /// <summary>
        /// 接收手机号码
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// 初始化一个<see cref="SmsTemplateBase"/>类型的实例
        /// </summary>
        /// <param name="template">短信模板</param>
        protected SmsTemplateBase(string template)
        {
            Template = template;
        }

        /// <summary>
        /// 设置 短信模板
        /// </summary>
        /// <param name="template">短信模板</param>
        public void SetTemplate(string template)
        {
            Template = template;
        }

        /// <summary>
        /// 添加参数
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        protected void Add(string key, object value)
        {
            if (string.IsNullOrEmpty(key))
            {
                return;
            }

            if (value ==null || string.IsNullOrEmpty(value.ToString()))
            {
                return;
            }

            ParamDict.Add(key, value);
        }

        /// <summary>
        /// 初始化参数值
        /// </summary>
        protected abstract void InitParamValue();

        /// <summary>
        /// 格式化模板
        /// </summary>
        /// <returns></returns>
        public string FormatTemplate()
        {
            InitParamValue();
            var tpl = Template;
            foreach (var param in ParamDict)
            {
                var value = param.Value.ToString();
                tpl = tpl.Replace(param.Key, value);
            }

            return tpl;
        }
    }
}
